import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from "../views/Login.vue"
import Register from "../views/Register.vue"
import Management from "../views/backstage/Management.vue"
import Navbar from "../components/backstage/Navbar.vue"
import UserManage from "../components/backstage/UserManage.vue"
import Header from "../components/backstage/Header.vue"
import FarmManage from "../components/backstage/FarmManage.vue"
import Sort from "../components/backstage/Sort.vue"
import ManageIndex from "../components/backstage/ManageIndex.vue"
import FormHouesManage from "../components/backstage/FormHouesManage.vue"
import TravelManage from "../components/backstage/TravelManage.vue"
import MoresManage from "../components/backstage/MoresManage.vue"
import CommentManage from "../components/backstage/CommentManage.vue"
import ExchangePostManaeg from "../components/backstage/ExchangePostManaeg.vue"
import HomeMenu from "../views/foreground/HomeMenu.vue"
import Home from "../components/foreground/Home.vue"
import FarmInfoPage from "../components/foreground/farmInfoPage.vue"
import HouseInfoPage from "../components/foreground/HouseInfoPage.vue"
import ScenicInfoPage from "../components/foreground/ScenicInfoPage.vue"
import FarmPage from "../components/foreground/FarmPage.vue"
import HouesPage from "../components/foreground/HouesPage.vue"
import ShopCart from "../components/foreground/ShopCart.vue"
import Communication from "../components/foreground/Communication.vue"
import ScenicSpots from "../components/foreground/ScenicSpots.vue"
import MoresCulture from "../components/foreground/MoresCulture.vue"
import Information from "../components/foreground/Information.vue"
import OrderInfo from "../components/foreground/OrderInfo.vue"
import UserIndex from "../components/foreground/profile/index.vue"
import UserAddress from "../components/foreground/UserAddress.vue"
import MyReservation from "../components/foreground/MyReservation.vue"
import PostDetailPage from "../components/foreground/PostDetailPage.vue"
import InformationManage from "../components/backstage/InformationManage.vue"
import ZnMessDetail from "../components/foreground/ZnMessDetail.vue"
import MyIssuePosts from "../components/foreground/MyIssuePosts.vue"
import MyFavorite from "../components/foreground/MyFavorite.vue"
import ConfirmAnOrder from "../components/foreground/ConfirmAnOrder.vue"
import OrderManage from "../components/backstage/OrderManage.vue"
import LogisticsManage from "../components/backstage/LogisticsManage.vue"
import HouseReservation from "../components/backstage/HouseReservation.vue"
import MoresInfoPage from "../components/foreground/MoresInfoPage.vue"
import MoresClassifycation from "../components/backstage/MoresClassifycation.vue"
import FarmEvaluate from "../components/backstage/FarmEvaluate.vue"
import HouseEvaluate from "../components/backstage/HouseEvaluate.vue"
import ScenicEvaluate from "../components/backstage/ScenicEvaluate.vue"

Vue.use(VueRouter)

const routes = [{
		path: "/",
		redirect: "/Home"
	},
	{
		path: '/HomeMenu',
		name: 'HomeMenu',
		component: HomeMenu,
		redirect: "/Home",
		children: [{
				path: '/Home',
				component: Home
			},
			{
				path: "/FarmInfoPage/:farmId",
				component: FarmInfoPage
			},
			{
				path: "/HouseInfoPage/:houseId",
				component: HouseInfoPage
			},
			{
				path: "/ScenicInfoPage/:spotId",
				component: ScenicInfoPage
			},
			{
				Titel: "跳转资讯详情页",
				path: "/ZnMessDetail/:znMessId",
				component: ZnMessDetail
			},
			{
				Titel: "跳转风俗文化详情页",
				path: "/MoresInfoPage/:moresId",
				component: MoresInfoPage
			},
			{
				path: "/FarmPage",
				component: FarmPage,
			},
			{
				path: "/HouesPage",
				component: HouesPage
			},
			{
				Titel: "跳转确认订单页",
				path: "/ConfirmAnOrder",
				component: ConfirmAnOrder
			},
			{
				path: "/ShopCart",
				component: ShopCart,
				meta: {
					requiresAuth: true
				}
			},
			{
				Title: "交流中心",
				path: "/Communication",
				component: Communication,
			},
			{
				Title: "帖子详情",
				path: "/PostDetailPage/:postId",
				component: PostDetailPage
			},
			{
				path: "/ScenicSpots",
				component: ScenicSpots
			},
			{
				path: "/MoresCulture",
				component: MoresCulture
			},
			{
				path: "/Information",
				component: Information,
				redirect: "/UserIndex",
				children: [{
						path: "/OrderInfo",
						component: OrderInfo,
						meta: {
							requiresAuth: true
						}
					},
					{
						path: "/UserIndex",
						component: UserIndex,
						meta: {
							requiresAuth: true
						}
					},
					{
						path: "/UserAddress",
						component: UserAddress,
						meta: {
							requiresAuth: true
						}
					},
					{
						path: "/MyReservation",
						component: MyReservation,
						meta: {
							requiresAuth: true
						}
					},
					{
						Title: "我的帖子",
						path: "/MyIssuePosts",
						component: MyIssuePosts,
						meta: {
							requiresAuth: true
						}
					},
					{
						Title: "我的收藏",
						path: "/MyFavorite",
						component: MyFavorite,
						meta: {
							requiresAuth: true
						}
					}
				],
				meta: {
					requiresAuth: true
				}
			}
		]
	},
	{
		path: '/Login',
		component: Login,
	},
	{
		path: '/Register',
		component: Register,
	},
	{
		path: '/Management',
		component: Management,
		redirect: "/ManageIndex",
		meta: {
			requiresRole: 'admin',
			requiresAuth: true
		},
		children: [{
				path: "/Navbar",
				component: Navbar,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				path: "/ManageIndex",
				component: ManageIndex,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				path: "/UserManage",
				component: UserManage,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				path: "/Header",
				component: Header,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				path: "/FarmManage",
				component: FarmManage,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				path: "/Sort",
				component: Sort,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				path: "/FormHoues",
				component: FormHouesManage,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				path: "/TravelManage",
				component: TravelManage,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				path: "/MoresManage",
				component: MoresManage,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				path: "/CommentManage",
				component: CommentManage,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				path: "/ExchangePostManaeg",
				component: ExchangePostManaeg,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				Title: "助农资讯",
				path: "/InformationManage",
				component: InformationManage,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				Title: "订单管理",
				path: "/OrderManage",
				component: OrderManage,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				Title: "快递物流",
				path: "/LogisticsManage",
				component: LogisticsManage,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				Title: "农家乐预定",
				path: "/HouseReservation",
				component: HouseReservation,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				Title: "风俗文化分类",
				path: "/MoresClassifycation",
				component: MoresClassifycation,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				Title: "农产品评价",
				path: "/FarmEvaluate",
				component: FarmEvaluate,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				Title: "农家乐评价",
				path: "/HouseEvaluate",
				component: HouseEvaluate,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
			{
				Title: "旅游景点点评",
				path: "/ScenicEvaluate",
				component: ScenicEvaluate,
				meta: {
					requiresRole: 'admin',
					requiresAuth: true
				}
			},
		]
	}
]

const router = new VueRouter({
	routes,
	mode: 'history'
})
router.beforeEach((to, from, next) => {
	const token = localStorage.getItem("token");
	const store = router.app.$store; // 获取 Vuex store 的引用  
	const userInfo = store.state.userInfo;
	if (to.meta.requiresAuth && !token) {
		// token 为空，且目标路由需要认证，阻止路由跳转  
		Vue.prototype.$message({
			message: '未登录，请登录',
			type: 'warning'
		});
		return next(false); // 使用 return 终止后续代码执行  
	}

	// 检查用户角色，如果路由需要特定角色且用户角色不符，则阻止跳转  
	if (to.meta.requiresRole && userInfo && userInfo.role !== to.meta.requiresRole) {
		Vue.prototype.$message({
			message: '您没有权限访问该页面',
			type: 'warning'
		});
		return next(false); // 使用 return 终止后续代码执行  
	}

	// 如果以上所有检查都通过，则允许路由跳转  
	next();
})

export default router